package me.zhengjie.modules.system.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.system.bo.AliResult;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cjl
 * @className CidianUtil
 * @description: TODO
 * @company: 上海明我信息技术有限公司
 * @date 2023年05月18日
 * @version: 1.0
 */
@Slf4j
public class CidianUtil {
    public static void main(String[] args) {
        AliResult ys = getCidian("罂粟");
        System.out.println(ys);
    }
    public static AliResult getCidian(String words) {
        String host = "https://jisucidian.market.alicloudapi.com/cidian/word?word="+words;
        Request request = new Request.Builder()
                .url(host)
                .header("Authorization", "APPCODE ec574939f6634d83ab44e5db6d9b2770" )
                .get().build();
        Response response = null;
        try {
            response = HttpAliUtil.getClientInstanc().newCall(request).execute();
            String res= response.body().string();
            log.info("getCidian result {}",res);
            AliResult parse = JSON.parseObject(res, AliResult.class);
            return parse;
            //处理成功
        } catch (IOException e) {
            log.error("getCidian request error ",e);
        }finally {
            if(response != null){
                try{
                    response.close();
                }catch (Exception e){
                    log.error("close response error", e);
                }

            }
        }
        return  null;
    }


}
